// Generate mutual fund holding file 
// Input: portfolio_stock /* fund-stock-semiannual holding */
use ../src/portfolio_stock.dta, clear
keep if reporttypeid>=5 

collapse (count) n=shares, by(fundid enddate )
keep if month(enddate)==6 | month(enddate)==12
gen date2 = mofd(enddate)
format date2 %tm
keep if date2<=ym(2015,6) & date2>=ym(2010,6)
drop n
sort fundid date2 enddate 
by fundid date2: keep if _n==_N
tempfile fund_ym
save `fund_ym'

use ../src/portfolio_stock.dta, clear
keep if reporttypeid>=5 
keep fundid symbol shares marketvalue proportion enddate

merge m:1 fundid enddate using `fund_ym'
keep if _m==3
drop _merge  

destring symbol, gen(code) force
drop if code==.
save ../dta/mf_hold, replace
